package com.handpay.zztong.hp.paymentbill.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.handpay.framework.notice.ApiNoticeFactory;
import com.handpay.framework.notice.ApiNoticeListener;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.framework.utils.DateUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.Bill;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.ui.TextViewTTF;
import com.handpay.zztong.hp.utils.AccountUtils;

public class TransResultFailureActivity extends ZZTongActivity {

    private TextView transaction_ScanAmount, transaction_MerchatAccount, transaction_MerchatSn, transaction_RecordTime, transaction_ConsumptionAccount, transaction_RecordNumber, transaction_OrderNumber, transaction_FailReason, txt_state;
    private TextViewTTF transaction_OrderName, txt_fail_reason;
    // private Button nextButton;
    private ImageView transaction_image, icon_state;
    private Button backButton;
    public static final String KEY_NAME = "card_in_name";
    private Bill mBill;
    private ScrollView mScrollView;

    private TextView businessInfo;
    private TextView transType;
    private TextView transSn;
    private TextView transTime;
    private TextView ConsumerCard;
    private TextView ConsumerTotal;
    private TextView transFailure;
    private TextView tv_trade_fail;//交易失败时候的提醒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_trans_result_failure);
        this.mAction = getIntent().getByteExtra(ACTION, Action_QueryBills);
        super.onCreate(savedInstanceState);
        mBill = (Bill) getIntent().getSerializableExtra(QueryBillsActivity.billvalue);
        HPLog.i(HPLog.SSX_TAG, "订单信息:" + mBill.toString());
        initView();
    }

    public void initView() {
        mScrollView = (ScrollView) this.findViewById(R.id.mScrollView);
        LinearLayout layout;
        if (mBill.getTradeType() == Bill.TRAD_TYPE_POSPAY) {
            layout = getUniPayView();
        } else {
            layout = getWebPayView();
        }
    }

    private LinearLayout getUniPayView() {
        LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_trans_unipay_failure_bill, null);
        mScrollView.addView(layout);
        businessInfo = (TextView) findViewById(R.id.businessInfotv);
        transType = (TextView) findViewById(R.id.transType);
        transSn = (TextView) findViewById(R.id.transSn);
        transTime = (TextView) findViewById(R.id.transTime);
        ConsumerCard = (TextView) findViewById(R.id.consumerCard);
        ConsumerTotal = (TextView) findViewById(R.id.transtotal);
        transFailure = (TextView) findViewById(R.id.transfailuretv);
        myActionBar.setTitleTextSize(20);
        if (mBill != null) {
            // 姓名
            this.businessInfo.setText(AccountUtils.getShopName(this));
            this.transType.setText(R.string.account_transfer);
            this.transSn.setText(mBill.getTransSN());
            if (TextUtils.isEmpty(mBill.getTransTime())) {
                this.transTime.setText(" ");
            } else {
                this.transTime.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(mBill.getTransTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
            }
            if (TextUtils.isEmpty(mBill.getTranMediaType())) {
                if (!TextUtils.isEmpty(mBill.getCardNoOut()) && !mBill.getCardNoOut().equals("null")) {
                    this.ConsumerCard.setText(mBill.getCardNoOut() + "");
                }
            } else {
                if (!TextUtils.isEmpty(mBill.getCardNoOut()) && !mBill.getCardNoOut().equals("null")) {
                    this.ConsumerCard.setText(mBill.getCardNoOut() + " /" + mBill.getTranMediaType());
                }
            }
            this.ConsumerTotal.setText(CommonUtils.formatMoney(mBill.getAmount() / 100));
            this.transFailure.setText(mBill.getTransStatus());
            if (Bill.STATUS_PROCESSING == mBill.getStatus()) {
                findViewById(R.id.failure_iv).setVisibility(View.GONE);
                findViewById(R.id.details_table).setVisibility(View.GONE);
                findViewById(R.id.details).setVisibility(View.VISIBLE);
                ((TextView) findViewById(R.id.details)).setText(mBill.getTransStatus());
            }
        }
        backButton = (Button) layout.findViewById(R.id.backButton);
        backButton.setOnClickListener(this);
        if (mAction == Action_QueryBills) {//流水查询页面隐藏返回按钮
            backButton.setVisibility(View.GONE);
        }else{
            backButton.setVisibility(View.VISIBLE);
        }
        return layout;
    }

    private LinearLayout getWebPayView() {
        LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_trans_webpay_failure_bill, null);
        mScrollView.addView(layout);
        /* *
         * 交易状态图标/交易状态文字描述/交易类型图片/交易金额/商户名称/商户编号/交易时间/消费账号/交易流水号/交易订单号/失败原因
		 */
        icon_state = (ImageView) layout.findViewById(R.id.icon_state);
        txt_state = (TextView) layout.findViewById(R.id.txt_state);
//        txt_fail_reason=(TextViewTTF)layout.findViewById(R.id.txt_fail_reason);
        if (mBill.getStatus() == 0) {//失败：更换失败图标，文案
            icon_state.setImageResource(R.drawable.trans_result_error);
            txt_state.setText(R.string.trans_fail2);
//            txt_fail_reason.setVisibility(View.GONE);
        } else if (mBill.getStatus() == 1) {//成功：更换成功图标，文案
            icon_state.setImageResource(R.drawable.trans_result_ok);
//            txt_state.setText(R.string.trans_success1);
        } else if (mBill.getStatus() == 2) {//处理中:更换图标，文案，隐藏失败原因tip
            icon_state.setImageResource(R.drawable.trans_result_ing);
            txt_state.setText(R.string.trans_processing);
//            txt_fail_reason.setVisibility(View.GONE);
        }
        transaction_image = (ImageView) layout.findViewById(R.id.transaction_image);
        transaction_ScanAmount = (TextView) layout.findViewById(R.id.transaction_ScanAmount);
        transaction_MerchatAccount = (TextView) layout.findViewById(R.id.transaction_MerchatAccount);
        tv_trade_fail = (TextView) layout.findViewById(R.id.tv_trade_fail);
        // transaction_MerchatAccount.setText(AccountUtils.getShopName());
        transaction_MerchatSn = (TextView) layout.findViewById(R.id.transaction_MerchatSn);
        transaction_RecordTime = (TextView) layout.findViewById(R.id.transaction_RecordTime);

        transaction_ConsumptionAccount = (TextView) layout.findViewById(R.id.transaction_ConsumptionAccount);

        transaction_RecordNumber = (TextView) layout.findViewById(R.id.transaction_RecordNumber);
        transaction_OrderName = (TextViewTTF) layout.findViewById(R.id.transaction_OrderName);
        transaction_OrderNumber = (TextView) layout.findViewById(R.id.transaction_OrderNumber);
        transaction_FailReason = (TextView) layout.findViewById(R.id.transaction_FailReason);
        // 返回图片
        backButton = (Button) layout.findViewById(R.id.backButton);
        backButton.setOnClickListener(this);
        if (mAction == Action_QueryBills) {//流水查询页面隐藏返回按钮
            backButton.setVisibility(View.GONE);
        }else{
            backButton.setVisibility(View.VISIBLE);
        }
        /***
         * transaction_image.setBackgroundResource（）方法会出现图片被压缩
         * 解决方法设置为transaction_image.setImageResource（）
         */

        if (mBill.getTradeType() == Bill.TRAD_TYPE_ALIPAY) {// 支付宝
            transaction_image.setImageResource(R.drawable.traderesult_alipay_icon);
            tv_trade_fail.setText(getString(R.string.traderesult_webpay_tip));
            transaction_OrderName.setText(getString(R.string.trans_alipay_order_name));
        } else if (mBill.getTradeType() == Bill.TRAD_TYPE_WEIPAY || mBill.getTradeType() == Bill.TRAD_TYPE_WEIPAY_SCAN) {// 微信
            transaction_image.setImageResource(R.drawable.traderesult_weipay_icon);
            tv_trade_fail.setText(getString(R.string.traderesult_webpay_tip));
            transaction_OrderName.setText(getString(R.string.trans_wxpay_order_name));
        } else {
            transaction_image.setImageResource(R.drawable.unionpay_icon);
        }

        transaction_ScanAmount.setText(CommonUtils.formatMoney(mBill.getAmount() / 100));
        transaction_MerchatAccount.setText(mBill.getShopName());
        transaction_MerchatSn.setText(mBill.getMerchantCode());
        transaction_RecordTime.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(mBill.getTransTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
        String orderNo = mBill.getOrderNo();
        if (!TextUtils.isEmpty(orderNo)) {
            transaction_OrderNumber.setText(mBill.getOrderNo());
        }
        String transferCardNo;
        if (TextUtils.isEmpty(mBill.getTranMediaType())) {
            transferCardNo = mBill.getCardNoOut();
        } else {
            transferCardNo = mBill.getCardNoOut() + " /" + mBill.getTranMediaType();
        }
        try {
            transaction_ConsumptionAccount.setText(CommonUtils.formattingCard(mBill.getTransAccount()));
        } catch (Exception e) {
            transaction_ConsumptionAccount.setText("");
        }

        transaction_RecordNumber.setText(mBill.getTransSN());
        transaction_FailReason.setText(mBill.getTransStatus());
        return layout;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.backButton) {
            finish();
        }
    }

    @Override
    protected boolean isShownTopBar() {
        return true;
    }

    @Override
    public ActionBar getActivityActionBar() {
        if (Action_QueryBills == mAction) {
            return new ActionBar(this.getString(R.string.trans_result_detial), true, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } else {
            return new ActionBar(this.getString(R.string.trans_result_confirm));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        String pageUrl = getIntent().getStringExtra("pageUrl");

        if (!TextUtils.isEmpty(pageUrl)) {
            Bundle bundle = new Bundle();
            bundle.putString("pageUrl", pageUrl);
            bundle.putString("status", "0");
            ApiNoticeFactory.getInstance().doBroadCastNotice(ApiNoticeListener.MARKET_ACTION, bundle);
        }
    }
}
